package cn.boyu.controller;


import cn.boyu.entity.AssetsTerminal;
import cn.boyu.entity.Customer;
import cn.boyu.entity.SysUser;
import cn.boyu.query.TerminalQuery;
import cn.boyu.result.AjaxResult;
import cn.boyu.service.IAssetsTerminalService;
import cn.boyu.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@RestController
@RequestMapping("/assetsTerminal")
@CrossOrigin
public class AssetsTerminalController {

    @Autowired
    private IAssetsTerminalService assetsTerminalService;

    @Autowired
    private ISysUserService sysUserService;

    //显示列表  /  高级查询
    @GetMapping("/query")
    public AjaxResult query(TerminalQuery qo) {
        IPage<AssetsTerminal> page = assetsTerminalService.queryPage(qo);
        return AjaxResult.success(page);
    }

    // 修改
    @PostMapping("/update")
    public AjaxResult update(AssetsTerminal assetsTerminal) {
        if (assetsTerminal.getId() != null) {
            assetsTerminalService.saveOrUpdate(assetsTerminal);
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error();
        }
    }

    //批量添加终端
    @PostMapping("/save")
    public AjaxResult save(Long firstId, Long lastId, AssetsTerminal assetsTerminal, HttpServletRequest request) {
        //获取当前登录用户
        String token = request.getHeader("token");
        SysUser user = sysUserService.getUserByToken(token);
        //添加操作人和创建时间
        assetsTerminal.setCreateTime(new Date());
        assetsTerminal.setUserId(user.getUserId());
        //批量添加方法
        assetsTerminalService.saveitions(firstId, lastId, assetsTerminal);
        return AjaxResult.success("添加成功");

    }

    //批量删除电表  (逻辑删除  只改变删除标识）
    @PostMapping("/delete")
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        Long[] arr = new Long[split.length];
        for (int i = 0; i < split.length; i++) {
            arr[i] = Long.parseLong(split[i]);
        }
        assetsTerminalService.batchDelete(arr);
        return AjaxResult.success("删除成功");
    }

    /*首页 统计信息接口*/
    @GetMapping("/count")
    public AjaxResult count(){
       int count = assetsTerminalService.count(new QueryWrapper<AssetsTerminal>().eq("del_flg",0));
       return AjaxResult.success(count);
    }
}